package com.donfu.web.controller.house;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseRegion;
import com.donfu.system.service.house.IHouseRegionService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

import javax.validation.constraints.NotNull;

/**
 * 行政区域Controller
 *
 * @author donfu
 * @date 2021-12-01
 */
@RestController
@RequestMapping("/house/region")
public class HouseRegionController extends BaseController
{
    @Autowired
    private IHouseRegionService houseRegionService;

    /**
     * 根据父类ID查询出下一级别数据  前台使用
     * @param id
     * @return
     */
    @GetMapping("/getChildrenList")
    public AjaxResult getByPid(@NotNull String id){
        HouseRegion houseRegion = new HouseRegion();
        houseRegion.setPid(Long.parseLong(id));
        List<HouseRegion> houseRegionList = houseRegionService.selectHouseRegionList(houseRegion);
        return AjaxResult.success(houseRegionList);
    }


    /**
     * 查询所有行政区域列表
     */
//    @PreAuthorize("@ss.hasPermi('house:region:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<HouseRegion> houseRegionAllList = new ArrayList<>();

        //所有数据
        List<HouseRegion> houseRegionList = houseRegionService.selectHouseRegionList(new HouseRegion());
        //根据级别分组，级别的值作为键名
        Map<Integer, List<HouseRegion>> collect = houseRegionList.stream().collect(Collectors.groupingBy(HouseRegion::getType));

        Integer provinceType = 1;
        //根据分类的值省级数据
        List<HouseRegion> provinceList = collect.get(provinceType);

        Integer cityType = 2;
        //市级数据
        List<HouseRegion> city = collect.get(cityType);
        // 根据父类id分组，也就是根据省级id分组
        Map<Long, List<HouseRegion>> cityListMap = city.stream().collect(Collectors.groupingBy(HouseRegion::getPid));

        Integer areaType = 3;
        //县级数据
        List<HouseRegion> areas = collect.get(areaType);
        // 根据父类id分组，也就是根据市级id分组
        Map<Long, List<HouseRegion>> areaListMap = areas.stream().collect(Collectors.groupingBy(HouseRegion::getPid));

        for (int i = 0; i < provinceList.size(); i++) {
            HouseRegion province = provinceList.get(i);

            // 从市级数据中查询出父类ID是当前省份的数据的集合
            List<HouseRegion> cityList = cityListMap.get(province.getId());
            List<HouseRegion> cityVOList = new ArrayList<>();
            // 循环遍历当前市级的集合，将区级数据注入到相应的市级对象中
            for (int j = 0; j <cityList.size() ; j++) {
                HouseRegion cityRegion = cityList.get(j);

                List<HouseRegion> areaList = areaListMap.get(cityRegion.getId());
                //List<HouseRegion> areaVOList = new ArrayList<>();
                //for (int k = 0; k <areaList.size() ; k++) {
                //    HouseRegion areaRegion = areaList.get(k);
                //    areaVOList.add(areaRegion);
                //}

                // 获取市下面的区域的集合
                cityRegion.setChildren(areaList);
                // 将市集合放到省的集合中
                cityVOList.add(cityRegion);

            }
            province.setChildren(cityVOList);
            houseRegionAllList.add(province);
        }


        return  AjaxResult.success(houseRegionAllList);
    }


/*
    // 上面那种方式更快，下面需要300ms的时间，上面只需要90ms
    @GetMapping("/getAll")
    public AjaxResult getAll(){
        // 获取最高的行政区域的数据
        HouseRegion region=new HouseRegion();
        region.setPid(0L);
        // 这个集合里都是没有上一级数据的根数据
        List<HouseRegion> regionList = houseRegionService.selectHouseRegionList(region);
        // 获取所有数据
        List<HouseRegion> allRegion = houseRegionService.selectHouseRegionList(null);
        //调用获取子数据集合的方法
        List<HouseRegion> childrenList = getChildrenList(regionList, allRegion);
        return  AjaxResult.success(childrenList);
    }

    public List<HouseRegion> getChildrenList(List<HouseRegion> houseRegionList,List<HouseRegion> allRegion){
        //当父类集合为空的时候，跳出循环
        if(houseRegionList==null || houseRegionList.size()==0){
            return null;
        }
        // 遍历父类集合
        for (HouseRegion houseRegion:houseRegionList){
            // 获取父类集合的id
            Long id = houseRegion.getId();
            // 用来存储遍历出来的子类数据的集合
            List<HouseRegion> houseRegions=new ArrayList<>();
            //遍历包含所有数据的集合，将pid的值等于父类id的遍历出来,就是找出子类
            for(HouseRegion region:allRegion){

                //  必须先转换成基本数据类型再比较，因为不转换为基本数据类型，值 >=128就算是值相等，一样不相等
                // Long id=128  ;  Long pid=128 ;   id==pid  false

                if(region.getPid().longValue()==id.longValue()){
                    houseRegions.add(region);
                }
            }
            // 迭代函数，将子类下面的所有数据取出来
            List<HouseRegion> childrenList = getChildrenList(houseRegions, allRegion);
            // 将遍历出来的子类集合存到对象中
            houseRegion.setChildren(childrenList);
        }
        // 返回处理好的集合
        return houseRegionList;
    }
    */

}
